﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Qianwei.Model;
using Qianwei.Biz;
using Qianwei.Common;

namespace Qianwei.Web
{
    public partial class MainPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetProductPicture();
                SetIntroContent();
                SetGrowthContent();
                SetNews();
                SetProductPics();
            }
        }

        #region Field
        private int introStrLeng = 300;
        #endregion

        private void SetProductPicture()
        {
            StringBuilder strphotos = new StringBuilder();
            IList<home_page_picture> homepagepictrues = HomePagePictureManager.Instance.QueryHomePagePicture(null, "Sort_Id");
            foreach (home_page_picture hpp in homepagepictrues)
            {
                strphotos.AppendFormat("<div class=\"photo\" hhref=\"{2}\" ><img src=\"{0}\" /><span>{1}</span></div>",
                    hpp.Picture_Path, hpp.Picture_Des, "HomePicture.aspx?pid=" + hpp.Home_Page_Id);
            }
            lphotos.Text = strphotos.ToString();
        }

        private void SetNews()
        {
            StringBuilder strexter = new StringBuilder();
            StringBuilder strnews = new StringBuilder();
            IList<news_center> externews = NewsCenterManager.Instance.QueryNewsCenter(10, "New_Type=" + (int)AppEnum.NewType.ExterReport, "New_Center_Id DESC");
            IList<news_center> companynews = NewsCenterManager.Instance.QueryNewsCenter(10, "New_Type=" + (int)AppEnum.NewType.CompanyNew, "New_Center_Id DESC");

            strexter.Append("<ul>");
            strnews.Append("<ul>");
            foreach (news_center nc in externews)
            {
                strexter.AppendFormat("<li><a target=\"_blank\" href=\"{0}\">{1}</a></li>",
                                    "News.aspx?nid=" + nc.New_Center_Id, nc.New_Title);
            }
            foreach (news_center nc in companynews)
            {
                strnews.AppendFormat("<li><a target=\"_blank\" href=\"{0}\">{1}</a></li>",
                                    "News.aspx?nid=" + nc.New_Center_Id, nc.New_Title);
            }
            strexter.Append("</ul>");
            strnews.Append("</ul>");

            lexterreport.Text = strexter.ToString();
            lcompanynews.Text = strnews.ToString();
        }

        private void SetIntroContent()
        {
            IList<page_info> pageinfo = PageInfoManager.Instance.QueryPageInfo(string.Format("Page_Url='{0}'", "Introduction.aspx"));
            if (pageinfo == null || pageinfo.Count == 0)
            {
                lqian_intro.Text = "load error...";
                return;
            }
            page_info pagenew = pageinfo.First();
            lqian_intro.Text = Util.SubStringContainHtml(pagenew.Page_Content, introStrLeng);
        }

        private void SetGrowthContent()
        {
            IList<page_info> pageinfo = PageInfoManager.Instance.QueryPageInfo(string.Format("Page_Url='{0}'", "GrowthCourse.aspx"));
            if (pageinfo == null || pageinfo.Count == 0)
            {
                lqian_growth.Text = "load error...";
                return;
            }
            page_info pagenew = pageinfo.First();
            lqian_growth.Text = Util.SubStringContainHtml(pagenew.Page_Content, introStrLeng);
        }

        private void SetProductPics()
        {
            int piccount = 9;
            List<product_info> productlistorigin = ProductInfoManager.Instance.QueryProductInfo("") as List<product_info>;
            if (productlistorigin == null || productlistorigin.Count == 0)
            {
                return;
            }
            //月饼
            var product_moon = productlistorigin.Where(s => s.Product_Type == (int)AppEnum.ProductType.MoonCake);
            if (product_moon != null || product_moon.Count() > 0)
            {
                product_moon = product_moon.OrderBy(s => s.Sort_Id).Take(piccount);
                StringBuilder strtext = new StringBuilder();
                foreach (product_info pic in product_moon)
                {
                    strtext.AppendFormat("<dl><dd><a target=\"_blank\" href=\"MoonCake.aspx?pid={2}\"><img alt=\"\" src=\"{0}\" /></a></dd><dt>{1}</dt></dl>", pic.Picture_Path, pic.Picture_Desc, pic.Product_Id);
                }
                lmooncake.Text = strtext.ToString();
            }
            //粽子
            var product_dumpling = productlistorigin.Where(s => s.Product_Type == (int)AppEnum.ProductType.Dumpling);
            if (product_dumpling != null || product_dumpling.Count() > 0)
            {
                product_dumpling = product_dumpling.OrderBy(s => s.Sort_Id).Take(piccount);
                StringBuilder strtext = new StringBuilder();
                foreach (product_info pic in product_dumpling)
                {
                    strtext.AppendFormat("<dl><dd><a target=\"_blank\" href=\"Dumplings.aspx?pid={2}\"><img alt=\"\" src=\"{0}\" /></a></dd><dt>{1}</dt></dl>", pic.Picture_Path, pic.Picture_Desc, pic.Product_Id);
                }
                lDumpling.Text = strtext.ToString();
            }
            //年货
            var product_newyear = productlistorigin.Where(s => s.Product_Type == (int)AppEnum.ProductType.NewYear);
            if (product_newyear != null || product_newyear.Count() > 0)
            {
                product_newyear = product_newyear.OrderBy(s => s.Sort_Id).Take(piccount);
                StringBuilder strtext = new StringBuilder();
                foreach (product_info pic in product_newyear)
                {
                    strtext.AppendFormat("<dl><dd><a target=\"_blank\" href=\"NewYear.aspx?pid={2}\"><img alt=\"\" src=\"{0}\" /></a></dd><dt>{1}</dt></dl>", pic.Picture_Path, pic.Picture_Desc, pic.Product_Id);
                }
                lNewYear.Text = strtext.ToString();
            }
            //套装
            var product_suit = productlistorigin.Where(s => s.Product_Type == (int)AppEnum.ProductType.Suit);
            if (product_suit != null || product_suit.Count() > 0)
            {
                product_suit = product_suit.OrderBy(s => s.Sort_Id).Take(piccount);
                StringBuilder strtext = new StringBuilder();
                foreach (product_info pic in product_suit)
                {
                    strtext.AppendFormat("<dl><dd><a target=\"_blank\" href=\"ProductSuit.aspx?pid={2}\"><img alt=\"\" src=\"{0}\" /></a></dd><dt>{1}</dt></dl>", pic.Picture_Path, pic.Picture_Desc, pic.Product_Id);
                }
                lSuit.Text = strtext.ToString();
            }
        }
    }
}